import {menu} from "@/dict/menuDict.js";

const modules = import.meta.glob('@/views/*/*.vue')

export function addMenuRouter(menuTreeStore,r){
    menuTreeStore.menuTree.forEach(item => {
        addMenuItemRouter([],item,r)
    })
}

function addMenuItemRouter(fatherArr,menuObj,r){
    if (menuObj.type === menu.type.FUNCTION){
        console.log(fatherArr,menuObj)
        let urlArr = menuObj.url.split('/')
        let temp = JSON.parse(JSON.stringify(fatherArr))
        temp.push(menuObj.name)
        r.addRoute(menuObj.father === "" ? 'main' : menuObj.father, {
            path:urlArr[urlArr.length - 1],
            name:urlArr[urlArr.length - 1],
            component: modules[`/src${menuObj.component}`],
            children:[],
            meta:{indexPath:temp,name:menuObj.name}
        })
        console.log(r.getRoutes())
    }
    if (menuObj.type === menu.type.MENU){
        fatherArr.push(menuObj.name)
        menuObj.child.forEach(item => {
            addMenuItemRouter(fatherArr,item,r)
        })
    }
}
